﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace Shop.Controllers
{
    public class HomeController : ApiController
    {
        // GET: api/Home
        #region 通用的方法，直接转到Index处理
        public HttpResponseMessage Get()
        {
            return Index.mainswith();
        }
        public HttpResponseMessage Post()
        {
            return Index.mainswith();
        }
        #endregion

        #region 上传附件用，调用必须要有uploadfile参数
        public async System.Threading.Tasks.Task<HttpResponseMessage> Post(string uploadfile)
        {

            if (!Request.Content.IsMimeMultipartContent())
            {
                return Common.HandleJson.ToJson("上传的数据格式不正确", false);
            }
            try
            {
                string root = "/TmpUpload";
                root = System.Web.HttpContext.Current.Server.MapPath(root);
                System.Net.Http.MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(root);
                await Request.Content.ReadAsMultipartAsync(provider);
                int ifilecount = provider.FileData.Count;
                if (ifilecount == 0)
                {
                    return Common.HandleJson.ToJson("没有找到上传的文件", false);
                }
                MultipartFileData filedata = provider.FileData[ifilecount - 1];
                //WPMC.Customer.saveImg.afterSave_CustomerHeadImg(file, idcus, out _errStr);
                //if (_errStr.Length > 0)
                //{
                //    return Common.HandleJson.ToJson(_errStr, false);
                //}
                IndexAttachment.mainswith(filedata);
            }
            catch (Exception ex)
            {
                //Common.handleLog.writeFile(ex.Message, System.Reflection.MethodBase.GetCurrentMethod());
                return Common.HandleJson.ToJson(ex.Message, false);
            }
            return Common.HandleJson.ToJson("成功", true);
        }
        #endregion
    }
}
